const jwt = require('jsonwebtoken')

const secreKey = 'cinema'

const sign = (data) =>
    jwt.sign(data, secreKey, {
        expiresIn: 60 * 60 * 24
    })

const authToken = (req, res, next) => {
    const token = req.headers['token']
    console.log(token)
    if (!token) return res.send({ message: '请提供token', code: 410 })
    jwt.verify(token, secreKey, (error) => {
        console.log(error)
        if (error) return res.send({ message: 'token已过期', code: 410 })
        next();
    })
}

module.exports = { sign, authToken }